//
//  XFXReadHomeViewController.m
//  XFXnews
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXReadHomeViewController.h"
#import "XFXSubscribeViewController.h"
#import "XFXReadViewController.h"
@interface XFXReadHomeViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *myScrollView;
@property (nonatomic, retain) UISegmentedControl *segmentC;
@property (nonatomic, retain) UIView *myViews;
@property (nonatomic, retain) XFXReadViewController *readViews;
@property (nonatomic, retain) XFXSubscribeViewController *readOneViews;

@end

@implementation XFXReadHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 初始化scrollView
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 60 * XFXSHEI, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.myScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height);
    self.myScrollView.delegate = self;
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.bounces = NO;
    [self.view addSubview:self.myScrollView];
    
    
    self.readViews = [[XFXReadViewController alloc] init];
    [self addChildViewController:self.readViews];
    [self.myScrollView addSubview:self.readViews.view];
    
    self.readOneViews = [[XFXSubscribeViewController alloc] init];
    [self addChildViewController:self.readOneViews];
    self.readOneViews.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.myScrollView addSubview:self.readOneViews.view];
    
    [self getSegmentC];
    
}

// 分段按钮封装
- (void)getSegmentC{
    
    self.myViews = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 4, 5 * XFXSHEI, self.view.frame.size.width / 2, 34 * XFXSHEI)];
    self.navigationItem.titleView = self.myViews;
    
    NSArray *array = [NSArray arrayWithObjects:@"推荐阅读", @"我的订阅", nil];
    
    self.segmentC = [[UISegmentedControl alloc] initWithItems:array];
    [self.segmentC setFrame:self.myViews.bounds];
    self.segmentC.selectedSegmentIndex = 0;
    self.segmentC.momentary = NO;
    [self.myViews addSubview:self.segmentC];
    // UIIimage是图片类,本身不能显示UIImageView显示图片(UIImage)的类
    // 设置分段按钮点击时间
    [self.segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
}

// 分段按钮实现方法
- (void)segmentAction:(UISegmentedControl *)sender{
    
    if (sender.selectedSegmentIndex == 0) {
        [self.myScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else{
        [self.myScrollView setContentOffset:CGPointMake(self.view.frame.size.width, 0) animated:YES];
    }
}

// scrollView 协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.myScrollView.contentOffset.x == 0) {
        self.segmentC.selectedSegmentIndex = 0;
    } else {
        self.segmentC.selectedSegmentIndex = 1;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
